﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinqToExcel;
using Remotion;

public partial class Admin_tiku_DaoRuShiTi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        bool fileOK = false;
        string path = Server.MapPath("~/上传文件/");
        if (System.IO.Directory.Exists(path) == false)//如果不存在就创建file文件夹
        {
            System.IO.Directory.CreateDirectory(path);
        }
        if (FileUpload1.HasFile)
        {
            String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] allowedExtensions = { ".xlsx", ".xls" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }
        if (fileOK)
        {
            try
            {
                string FileName = path + FileUpload1.FileName;
                FileUpload1.SaveAs(FileName);//上传
                //LabMessage1.Text = "文件上传成功.";
                //LabMessage2.Text = "<b>原文件路径：</b>" + FileUpload1.PostedFile.FileName + "<br />" +
                //              "<b>文件大小：</b>" + FileUpload1.PostedFile.ContentLength + "字节<br />" +
                //              "<b>文件类型：</b>" + FileUpload1.PostedFile.ContentType + "<br />";


                ExcelQueryFactory excel = new ExcelQueryFactory(FileName);//(@"D:\Book1.xls");
                int WorksheetCount = excel.GetWorksheetNames().Count();
                string row = string.Empty;
                List<object> cell = new List<object>();

                using (DataClassesDataContext ds = new DataClassesDataContext())
                {
                    if (WorksheetCount > 0)
                    {
                        IQueryable<Row> query = from p in excel.Worksheet(0)
                                                select p;
                        int count = 0;//记录导入了多少条记录
                        foreach (Row item in query)
                        {
                            SingleProblem p = new SingleProblem();
                            
                            p.c_id =Convert.ToInt32( DropDownList1.SelectedValue);
                            p.Title = Convert.ToString(item[0].Value);
                            p.AnswerA = Convert.ToString(item[1].Value);
                            p.AnswerB = Convert.ToString(item[2].Value);
                            p.AnswerC = Convert.ToString(item[3].Value);
                            p.AnswerD = Convert.ToString(item[4].Value);
                            p.Answer = Convert.ToString(item[5].Value);                            
                            ds.SingleProblem.InsertOnSubmit(p);

                            count++;
                            //int j = 0;
                            //foreach (string col in item.ColumnNames)
                            //{
                            //    if (string.IsNullOrEmpty(row))
                            //        row = col + "={" + j.ToString() + "}";
                            //    else
                            //        row += "," + col + "={" + j.ToString() + "}";
                            //    cell.Add(item[j]);

                            //    j++;
                            //}//for col   
                            //Response.Write(string.Format(row, cell.ToArray()) + "<br/>");
                            //row = string.Empty;
                            //cell.Clear();

                        }//for item
                        ds.SubmitChanges();

                        GridView1.DataSource = (from q in ds.SingleProblem
                                                orderby q.ID descending
                                                select q).Take(count);
                        GridView1.DataBind();
                    }//for WorksheetCount
                }
            }
            catch (Exception ex)
            {
                //LabMessage1.Text = "文件上传不成功.";
            }
        }
        else
        {
            //LabMessage1.Text = "请检查选择的文件是否 Excel文件";
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}